# Contributor: V.Krishn <vkrishn4@gmail.com>
# Contributor: Eivind Uggedal <eu@eju.no>
# Contributor: TBK <alpine@jjtc.eu>
# Maintainer: fossdd <fossdd@pwned.life>
pkgname=redict
pkgver=7.3.1
pkgrel=0
pkgdesc="A distributed key/value database"
url="https://redict.io/"
arch="all"
license="LGPL-3.0-only"
pkgusers="redict"
pkggroups="redict"
makedepends="linux-headers openssl-dev>3"
checkdepends="tcl procps"
install="
	$pkgname-compat.pre-install
	$pkgname.post-install
	$pkgname.post-upgrade
	$pkgname.pre-install
	"
subpackages="
	$pkgname-benchmark
	$pkgname-cli
	$pkgname-compat::noarch
	$pkgname-openrc
	"
builddir="$srcdir/$pkgname"
source="redict-$pkgver.tar.gz::https://codeberg.org/redict/redict/archive/$pkgver.tar.gz
	redict.conf.patch
	sentinel.conf.patch
	$pkgname.initd
	$pkgname.confd
	$pkgname-sentinel.initd
	$pkgname.logrotate
	"

case "$CARCH" in
	# FIXME: One test is broken on s390x.
	s390x) options="!check";;
	# known test failure
	*) options="!check";;
esac

# secfixes:
#   7.3.1-r0:
#     - CVE-2024-31227
#     - CVE-2024-31228
#     - CVE-2024-31449

build() {
	export CFLAGS="$CFLAGS -DUSE_MALLOC_USABLE_SIZE -O2 -flto=auto"
	make USE_JEMALLOC=no \
		MALLOC=libc \
		BUILD_TLS=yes \
		all
}

check() {
	make test
}

package() {
	make install PREFIX="$pkgdir/usr" INSTALL_BIN="$pkgdir/usr/bin"

	cd "$pkgdir"

	install -D -m 644 "$builddir"/redict.conf etc/redict.conf

	# This file must be writable for redict, otherwise Sentinel fails to start.
	install -D -m 644 -o redict -g redict "$builddir"/sentinel.conf etc/sentinel.conf

	install -D -m 755 "$srcdir"/redict.initd etc/init.d/redict
	install -D -m 755 "$srcdir"/redict-sentinel.initd etc/init.d/redict-sentinel
	install -D -m 644 "$srcdir"/redict.confd etc/conf.d/redict
	install -D -m 644 "$srcdir"/redict.logrotate etc/logrotate.d/redict

	install -d -o redict -g redict \
		var/lib/redict \
		var/log/redict
}

benchmark() {
	pkgdesc="Redict benchmarking tool"

	amove usr/bin/redict-benchmark
}

cli() {
	pkgdesc="Redict CLI client"

	amove usr/bin/redict-cli
}

compat() {
	pkgdesc="Redict command symlinks and system group for compatibility with redis"
	depends="!redis !valkey-compat"

	mkdir -p "$subpkgdir"/usr/bin
	for i in benchmark check-aof check-rdb cli sentinel server; do
		ln -s /usr/bin/redict-$i "$subpkgdir"/usr/bin/redis-$i
	done
}

sha512sums="
79d174c0f58c73a52f3b0225280f69f92790f8d43cdf70a2353cefd06d73030ffb5e3eb30e06dcd9c47161f2d43a09cca6dc815e9dd83b620716c2ae3243d3bc  redict-7.3.1.tar.gz
96740880cd0caac2cecb11066a555055e7c3f10feb312b2d4c7a206fb92c1003415cf7ab81bec4ac96f7631724d9097b1ca130621c8b564a7987dcd75d2f6e25  redict.conf.patch
a3cec12566097fe78a31d09db77ca956ba52ee1fbb8ac4f5ffc49d0299e29f94ef55830f7760324a875bd47391eee116e0c4efb4bef00d6045bf98b81f392dfc  sentinel.conf.patch
555edff7dd7768c9579a101df86e91cc14cb9941365f051e0e993f1c341cd2b46fcfab6f4a6fd4c1d589242363e323734b453652d687d2d46cb651d84d42caee  redict.initd
4a0b04388115b5af6d4173aca66b081740432c19e1085c0bd5251fbbfb190c06aaa0540575e160c1e907cf504e33b3dcb67eb916dda3347cf394123d64adffb2  redict.confd
7cc571f9f977a167d61bd952cf74d043ef17e5211113928c77a2f088df1cc8f31dd19e7de79e2795479f7afccd4dd32b66e61b67c99a21ffa8d9560811185728  redict-sentinel.initd
68e0b0c4c8def5d42149b895ac681d91b8c691a5fd90bce64e6bf6c7780f33c6ac02a923642ebdc85f6bb41d8039cc38b7d3be49e5e9f86f097e3d1f6d41ecd4  redict.logrotate
"
